Component({
  mixins: [],
  data: {
    previewData: {},
    onlineData: {},
    activityData: {},
    previewUrl: '',
    onlineUrl: ''
  },
  props: {},
  didMount() {
    this.loadData();
    this.loadInstance()
  },
  didUpdate() { },
  didUnmount() { },
  methods: {

    copyPreviewCode() {
      my.setClipboard({
        text: this.data.previewData.pre_view_url
      });
      my.alert({
        content: '复制成功！'
      })
    },
    copyOnlineCode() {
      my.setClipboard({
        text: this.data.onlineData.online_url
      });
      my.alert({
        content: '复制成功！'
      })
    },
    async intanceCode() {
      const { cloud } = getApp();
      try {
        const res = await cloud.topApi.invoke({
          api: 'taobao.miniapp.template.instantiate',
          authScope: '*',
          data: {
            clients: 'taobao,tmall',
            description: this.data.activityData.shop_title + '拆礼盒活动欢迎你',
            ext_json: `{'shop_id':${this.data.activityData.shop_id}}`,
            icon: this.data.activityData.shop_logo,
            name: this.data.activityData.user_nick,
            template_id: '3000000002163832',
            template_version: '0.0.14'
          }
        });
        this.setData({
          previewData: res
        })
       this.writeInstance()


      } catch (e) {
        my.alert({ content: 'error ' + e.message })
      }
    },
    async updateCode() {
      const { cloud } = getApp();
      try {
        const res = await cloud.topApi.invoke({
          api: 'taobao.miniapp.template.updateapp',
          authScope: '*',
          data: {
            clients: 'taobao,tmall',
            id: this.data.previewData.app_id,
            ext_json: `{'shop_id':${this.data.activityData.shop_id}}`,
            template_id: '3000000002163832',
            template_version: '0.0.14'
          }
        });
        this.setData({
          previewData: res,
        })
        this.writeInstance()

      } catch (e) {
        my.alert({ content: 'error ' + e.message })
      }
    },
    async submitCode() {
      const { cloud } = getApp();
      try {
        const res = await cloud.topApi.invoke({
          api: 'taobao.miniapp.template.onlineapp',
          authScope: '*',
          data: {
            clients: 'taobao,tmall',
            app_id: this.data.previewData.app_id,
            template_id: '3000000002163832',
            template_version: '0.0.14',
            app_version: this.data.previewData.app_version
          }
        });
        this.setData({
          onlineData: res.app_info
        })

      } catch (e) {
        my.alert({ content: 'error ' + e.message })
      }
    },
    async loadData() {
      const { cloud } = getApp();
      const res = await cloud.application.httpRequest({
        path: '/index.php/index/qn/getset',
        method: 'GET',
        params: { shop_id: getApp().globalData.shop_id }
      })

      if (res) {
        this.setData({
          activityData: res
        })
      }
    },
    async loadInstance() {
      const { cloud } = getApp();
      const res = await cloud.application.httpRequest({
        path: '/index.php/index/qn/getinstance',
        method: 'GET',
        params: { shop_id: getApp().globalData.shop_id }
      })

      if (res) {
        this.setData({
          previewData: res
        })
      }
    },
    async writeInstance() {
      const { cloud } = getApp();
      let sendData = {
        shop_id: getApp().globalData.shop_id,
        appkey: this.data.previewData.appkey,
        app_name: this.data.previewData.app_name,
        app_description: this.data.previewData.app_description,
        app_icon: this.data.previewData.app_icon,
        app_id: this.data.previewData.app_id,
        pre_view_url: this.data.previewData.pre_view_url,
        app_version: this.data.previewData.app_version,
      };
      const res = await cloud.application.httpRequest({
        path: '/index.php/index/qn/writeinstance',
        method: 'POST',
        params: sendData
      })
    },
  },

});
